@extends('layout.layer')

@section('layout.content')
  <div class="space-18"></div>
  <div class="row">
    <div class="col-xs-12">
      <form id="question-create" autocomplete="off">
          <input type="hidden" name="tid" value="{{$tid}}">
        <div class="form-group row">
          <label class="col-xs-12 control-label" for="title"> <b><span class="red">*</span> 题目：</b>
          </label>
          <div class="col-xs-12">
            <input type="text" class="form-control" name="title" placeholder="题目"/>
          </div>
        </div>

        <div class="form-group row">
          <label class="col-xs-12 control-label" for="number"> <b><span class="red">*</span> 选项：</b>
          </label>
          <div class="col-xs-12">
            <table class="table table-hover table-striped table-bordered nonborder">
              <tbody>
                @for($i=1;$i<=4;$i++)
                <tr class="bank">
                  <td class="align-center">
                    <input name="correct[]" type="checkbox" value="1" class="ace ace-checkbox-2 input-lg">
                    <span class="lbl"> </span>
                  </td>
                  <td class="align-left">
                    <input name="content[]" type="text" class="form-control" placeholder="答案选项"/>
                  </td>
                </tr>
                @endfor
              </tbody>
            </table>
          </div>
        </div>

        <div class="space-32"></div>
        <div class="form-group row">
          <div class="col-xs-12 center">
            <button type="submit" class="btn btn-primary" style="margin-right:15px;">
              <i class="ace-icon fa fa-check"></i>
              确定
            </button>

            <button type="button" class="btn btn-light">
              <i class="ace-icon fa fa-times"></i>
              取消
            </button>
          </div>
        </div>

      </form>
    </div>
  </div>
@stop

@section('javascript')

  <script src="//cdn.bootcss.com/jquery-validate/1.19.1/jquery.validate.min.js"></script>
  <script src="{{URL::asset('assets/js/jquery-validate-methods.js')}}"></script>
  <script src="//cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.js"></script>


  <script type="text/javascript">
      $(function () {

        $('.btn-light').on('click', function(e) {  
            //parent.location.reload();
            //var index = parent.layer.getFrameIndex(window.name); 
            parent.layer.close(parent.layer.index);            
          });
          /**
           表单前端验证
           */
          $("#question-create").validate({
              rules: {
                  title: {required: true},
                  type: {required: true},
              },
              messages: {
                  title: {required: "请填写题目标题"},
              },


              /* 失去焦点时不验证 */
              onfocusout: false,
              onkeyup: false,
              errorElement: 'div',
              errorClass: 'help-block',

              success: function (e) {
                  $(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
                  $(e).remove();
              },

              showErrors: function (errorMap, errorList) {
                  $.each(errorList, function (i, v) {
                      layer.tips(v.message, v.element, {tips: [3,'#dd0000'], time: 2000});
                      return false;
                  });

              },

              submitHandler: function (form) {
                var content = [];
                $("input[name^='content']").each(function(i, el) {
                  content.push($(this).val());
                });
                console.log(content);
                var correct = [];
                $("input:checkbox[name^='correct']").each(function(i, el) {
                  if($(this).is(":checked")){
                    correct.push("1");
                  }
                  else{
                    correct.push("0");
                  }
                });
                console.log(correct);
                var tid = $("input[name='tid']").val();
                var uri = '/act/question/'+tid+'/store';
                console.log(uri);
                  $.post(uri, {
                      '_token': $("input[name='_token']").attr('value'),
                      'title': $("input[name='title']").val(),
                      'content':content,
                      'correct':correct,
                      'type':1,//选择题
                      'is_submit': true
                  }, function (data) {
                      console.log(data);
                      layer.msg(data.message, {time: 1000,shift: -1}, function () {
                            if (data.status === true) {
                              parent.location.reload();
                              parent.layer.close(parent.layer.index);
                          }
                      });

                  }, 'json').error(function (data) {
                      layer.msg(data.responseJSON.message);
                  });
              }


          });

      });
  </script>
@stop